package cn.innosmart.aq.manager;

import android.text.TextUtils;

import com.tutk.p2p.ConnectionEntity;
import com.tutk.p2p.ResponseHandler;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.innosmart.aq.bean.AquariumBean;
import cn.innosmart.aq.bean.ScheduleBean;
import cn.innosmart.aq.util.SystemConstant;

/**
 * Created by David on 2015/7/1.
 */
public class ScheduleManager extends BaseManager {
    private static AddCallBack addCallBack;
    private static UpdateCallBack updateCallBack;
    private static RemoveCallBack removeCallBack;
    private static ListCallBack listCallBack;
    private String Tag = "ScheduleManager";
    private ResponseHandler.ResponseCallback mScheduleManagerAddCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "ScheduleManager.Add get response=" + message);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    String id = jRoot.getJSONObject("result").getString("id");
                    if (addCallBack != null) {
                        addCallBack.onAddResponseCallBack(0, id);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    private ResponseHandler.ResponseCallback mScheduleManagerUpdateCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "ScheduleManager.Update get response=" + message);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    String id = jRoot.getJSONObject("result").getString("id");
                    if (updateCallBack != null) {
                        updateCallBack.onUpdateResponseCallBack(0, id);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    private ResponseHandler.ResponseCallback mScheduleManagerRemoveCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "ScheduleManager.Remove get response=" + message);
            JSONObject jRoot = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    if (removeCallBack != null) {
                        removeCallBack.onRemoveResponseCallBack(0, null);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
    private ResponseHandler.ResponseCallback mScheduleManagerListCallBack = new ResponseHandler.ResponseCallback() {
        @Override
        public void onResponseCallback(ConnectionEntity connectionEntity, String message) {
            log(Tag, "ScheduleManager.List get response=" + message);
            JSONObject jRoot = null;
            ArrayList<Object> scheduleList = null;
            try {
                jRoot = new JSONObject(message);
                int rspCode = jRoot.getInt("code");
                String rspMsg = jRoot.getString("message");
                if (0 == rspCode && "Success".equals(rspMsg)) {
                    JSONArray jsonArray = jRoot.getJSONObject("result").getJSONArray("schedules");
                    scheduleList = new ArrayList<Object>();
                    for (int i = 0; i < SystemConstant.aquariumList.size(); i++) {
                        SystemConstant.aquariumList.get(i).getScheduleList().clear();
                    }
                    //将获取的场景信息初始化并存入内存
                    for (int i = 0; i < jsonArray.length(); i++) {
                        ScheduleBean scheduleBean = new ScheduleBean((JSONObject) jsonArray.getJSONObject(i));
                        String place = scheduleBean.getPlace();
                        if (!TextUtils.isEmpty(place)) {
                            AquariumBean aquariumBean = AquariumBean.findAquariumById(place);
                            aquariumBean.getScheduleList().add(scheduleBean);
                            System.out.println("ScheduleSize=" + aquariumBean.getScheduleList().size());
                        }
                        scheduleList.add(scheduleBean);
                    }
                    if (listCallBack != null) {
                        listCallBack.onListResponseCallBack(0, scheduleList);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    private ScheduleManager() {
    }

    public static ScheduleManager getInstance() {
        return ScheduleManagerInstance.instance;
    }

    // ScheduleManager.Add
    public boolean Add(ConnectionEntity connectionEntity, String params, AddCallBack addCallBack) {
        log(Tag, "Add. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.addCallBack = addCallBack;
        long callid = System.currentTimeMillis() / 1000;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mScheduleManagerAddCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "ScheduleManager.Add", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // ScheduleManager.Update
    public boolean Update(ConnectionEntity connectionEntity, String params, UpdateCallBack updateCallBack) {
        log(Tag, "UPdate. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.updateCallBack = updateCallBack;
        long callid = System.currentTimeMillis() / 1000;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mScheduleManagerUpdateCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "ScheduleManager.Update", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // ScheduleManager.Remove
    public boolean Remove(ConnectionEntity connectionEntity, String params, RemoveCallBack removeCallBack) {
        log(Tag, "Remove. UID = " + connectionEntity.getUid() + ", params = " + params);
        this.removeCallBack = removeCallBack;
        long callid = System.currentTimeMillis() / 1000;
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mScheduleManagerRemoveCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "ScheduleManager.Remove", params, callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // ScheduleManager.List
    public boolean List(ConnectionEntity connectionEntity, ListCallBack callBack) {
        log(Tag, "List. UID = " + connectionEntity.getUid());
        this.listCallBack = callBack;
        long callid = System.currentTimeMillis() / 1000;
        JSONObject jParams = new JSONObject();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mScheduleManagerListCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "ScheduleManager.List", jParams.toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    // ScheduleManager.List
    public boolean List(ConnectionEntity connectionEntity, ListCallBack callBack, int delay) {
        log(Tag, "List. UID = " + connectionEntity.getUid());
        this.listCallBack = callBack;
        long callid = System.currentTimeMillis() / 1000;
        if (delay != 0)
            callid = callid + delay;
        JSONObject jParams = new JSONObject();
        ResponseHandler handler = new ResponseHandler();
        handler.setCallback(mScheduleManagerListCallBack);
        connectionEntity.mReqRspHashMap.put(callid, handler);
        if (!SendCommand(connectionEntity, connectionEntity.getUid(), "ScheduleManager.List", jParams.toString(), callid)) {
            connectionEntity.mReqRspHashMap.remove(callid);
            return false;
        } else {
            return true;
        }
    }

    public interface AddCallBack {
        void onAddResponseCallBack(int ret, String id);
    }

    public interface UpdateCallBack {
        void onUpdateResponseCallBack(int ret, String ruleid);
    }

    public interface RemoveCallBack {
        void onRemoveResponseCallBack(int ret, String ruleid);
    }

    public interface ListCallBack {
        void onListResponseCallBack(int ret, ArrayList<Object> childList);
    }

    private static class ScheduleManagerInstance {
        private static final ScheduleManager instance = new ScheduleManager();
    }


}
